package org.example.cake.service.impl;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.example.cake.entity.Discount;
import org.example.cake.mapper.DiscountMapper;
import org.example.cake.service.DiscountService;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DiscountServiceImpl implements DiscountService{

    @Resource
    private DiscountMapper discountMapper;

    @Override
    public int deleteByPrimaryKey(Integer discountid) {
        return discountMapper.deleteByPrimaryKey(discountid);
    }

    @Override
    public int insert(Discount record) {
        return discountMapper.insert(record);
    }

    @Override
    public int insertSelective(Discount record) {
        return discountMapper.insertSelective(record);
    }

    @Override
    public Discount selectByPrimaryKey(Integer discountid) {
        return discountMapper.selectByPrimaryKey(discountid);
    }

    @Override
    public int updateByPrimaryKeySelective(Discount record) {
        return discountMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Discount record) {
        return discountMapper.updateByPrimaryKey(record);
    }

    /**
     * 查询所有优惠券
     * @return 优惠券集合
     * 徐
     */
    public List<Discount> selectAllDiscount(String discountname,Integer status){
        return discountMapper.selectAllDiscount(discountname,status);
    }
    /**
     * 通过userid查询所拥有的优惠券
     * @param userid 用户id
     * @return 优惠券集合
     */
    public List<Discount> selectDiscountByUserid(@Param("userid") Integer userid){
        return discountMapper.selectDiscountByUserid(userid);
    }


    /**
     * //新增后台优惠券信息
     * @author 徐
     * @param discount 优惠券对象
     * @date 2025-08-01
     * @return 受影响的行数
     */
    @Override
    public String insertDiscountEnd(Discount discount) {
        return discountMapper.insertDiscountEnd(discount)>0?"新增成功":"新增失败";
    }

    /**
     * //后台优惠券删除（根据id删除）
     * @author 徐
     * @param discountid 优惠券id
     * @date 2025-08-01
     * @return 优惠券id
     */
    @Override
    public String deleteDiscountEnd(Integer discountid) {
        return discountMapper.deleteDiscountEnd(discountid)>0?"删除成功":"删除失败";
    }

    /**
     * //修改后台优惠券信息
     * @author 徐
     * @param discount 优惠券对象
     * @date 2025-08-01
     * @return 受影响的行数
     */
    @Override
    public String updateDiscountEnd(Discount discount) {
        return discountMapper.updateDiscountEnd(discount)>0?"修改成功":"修改失败";
    }
    /**
     * 第一次登录得到的优惠券
     * @return 优惠券集合
     */
    public List<Discount> selectTwoDiscount(){
        return  discountMapper.selectTwoDiscount();
    }

    @Override
    public Discount getyouhuiquan(Integer discountId) {
        return discountMapper.selectById(discountId);
    }

    //根据openid新增优惠券
    @Override
    public Discount createDiscountForNewUser(String openid) {
        Discount discount = new Discount();
        discount.setDiscountname("新人券");
        discount.setMoney(10);
        discount.setMaxmoney(100);
        discount.setStarttime(Timestamp.valueOf(LocalDateTime.now()));
        discount.setEndtime(Timestamp.valueOf(LocalDateTime.now().plusDays(3))); //结束时间加上3天
        discount.setStatus(0);// 0是全场通用
        // 插入数据库
        discountMapper.insertDiscount(discount);
        // 获取刚刚插入的 优惠券id
        Integer discountId=discount.getDiscountid();
        System.out.println("生成的优惠券id:"+discountId);
        // 根据优惠券id查询出优惠券信息
        Discount selectById = discountMapper.selectById(discountId);
        System.out.println("selectById = " + selectById);
        return discount;
    }


    /**
     * 根据优惠券id查询优惠券
     * @param discountid 优惠券id
     * @return 优惠券实体
     */
    public Discount selectById(@Param("discountid") Integer discountid){
        return discountMapper.selectById(discountid);
    }

}
